package com.scraper.articles.seekingalpha;

import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.scraper.articles.domain.Article;
import com.scraper.articles.domain.filter.ArticleFilter;
import com.scraper.articles.exception.ScraperException;
import com.scraper.articles.scanner.BaseJsoupScanner;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;

public class SeekingAlphaScanner extends BaseJsoupScanner<Article, ArticleFilter> {

    private int numberOfItemsToSkip = 0;

    @Override
    public String getSiteURL() {
        return "http://seekingalpha.com/symbol/";
    }

    @Override
    protected String applyFilter(String siteURL, ArticleFilter filter) {
        String searchText = filter.getSearchText().replaceAll("\\s+", "%20");
        return siteURL + "/" + searchText + "/focus";
    }

    @Override
    protected Document getPage(int pageNumber, String baseURI) {
        if (pageNumber == 1) {
            HtmlPage htmlPage = getHtmlPage(baseURI);
            return parseHtml(htmlPage.asXml());
        }
        List<?> byXPath = lastRequestedPage.getByXPath("//div[@id='in_focus_articles_show_more']//a");
        if (byXPath.isEmpty()) {
            return parseHtml(lastRequestedPage.asXml());
        }
        HtmlAnchor htmlAnchor = (HtmlAnchor) byXPath.get(0);
        try {
            lastRequestedPage = htmlAnchor.click();
        } catch (IOException e) {
            throw new ScraperException(e);
        }
        waitForScript(10000);
        numberOfItemsToSkip = (pageNumber - 1) * 20;

        return parseHtml(lastRequestedPage.asXml());
    }

    @Override
    protected int getNumberOfItemsToSkip() {
        return numberOfItemsToSkip;
    }

    @Override
    protected boolean processNextPage(Document doc) {
        return doc.getElementById("in_focus_articles_show_more") != null;
    }

    @Override
    protected boolean isProcessCurrentPage(Document doc) {
        Elements elements = doc.select("no_results");
        return elements.isEmpty() || numberOfItemsToSkip >= getDataList().size();
    }

    @Override
    protected Callable<Article> getDataScraper(Element element) {
        return new SeekingAlphaScraper(element, webClient);
    }

    @Override
    protected Elements getElements(Document doc) {
        return doc.select("#headlines_in_focus_articles .symbol_articles_list .symbol_article > a[sasource=qp_focused");
    }
}
